// pages/sale/sendGoods/sendGoods.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderGoods: [],
    codeContent: [],
    codeTotal: 0, //成功扫了几个码
    nopur: null, //我的订单转采购的标识
    antiIds:[]
  },
  //处理扫码成功后的结果
  dealScanResult: function(obj, codeContent, index, nindex, res) {
    let that = this
    let arr = codeContent
    let antiList = that.data.antiIds
    console.log(arr[index] == undefined)
    if (obj.errno === 0) {
      if (arr[index] == undefined) {
        arr[index] = new Array()
      }
      arr[index][nindex] = res.result
      let codetotal = that.data.codeTotal + 1
      antiList.push(obj.data)
      console.log('赋值之后')
      console.log('codetotal:' + codetotal)
      console.log(arr[index][nindex])
      console.log(arr)
      that.setData({
        codeContent: arr,
        codeTotal: codetotal,
        antiIds: antiList
      })
      console.log('antiList:*****')
      console.log(antiList)
      console.log(that.data.codeContent)
      console.log('codeTotal:' + that.data.codeTotal)
    } else {
      util.showErrorToast(obj.errmsg)
    }
  },
  // 扫二维码
  scanCode: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let nindex = e.currentTarget.dataset.nindex
    let orderInfo = wx.getStorageSync('orderInfo')
    let goodsId = e.currentTarget.dataset.goodsid
    console.log('扫码结果')
    console.log(e)
    console.log('index:' + e.currentTarget.dataset.index)
    console.log('nindex:' + e.currentTarget.dataset.nindex)
    console.log('goodsId:' + e.currentTarget.dataset.goodsid)
    console.log(orderInfo)
    // let arr = this.data.codeContent
    console.log(that.data.codeContent)

    wx.scanCode({
      success(res) {
        console.log(res)
        console.log(res.result)
        if ('nopur' == that.data.nopur) {
          util.request(api.SendGoodsOfScanCode, {
            goodsId: parseInt(goodsId),
            url: res.result
          }).then(function(obj) {
            console.log(obj)
            that.dealScanResult(obj, that.data.codeContent, index, nindex, res)
          })
          return;
        }
        console.log('扫码收货')
        util.request(api.SaleScanCode, {
          consigneeId: parseInt(orderInfo.userId),
          goodsId: parseInt(goodsId),
          url: res.result,
          orderId: parseInt(orderInfo.id)
        }).then(function(obj) {
          console.log(obj)
          that.dealScanResult(obj, that.data.codeContent, index, nindex, res)
          // console.log('length:' + that.data.orderGoods[index].number);
          // if (that.data.orderGoods[index].number-- >=1 ){
          //   console.log('递归开始')
          //   that.scanCode()
          // }
          // if (obj.errno === 0) {
          //   if (arr[index] == undefined) {
          //     arr[index] = new Array()
          //   }
          //   arr[index][nindex] = res.result
          //   let codetotal = that.data.codeTotal + 1
          //   console.log('赋值之后')
          //   console.log('codetotal:' + codetotal)
          //   console.log(arr[index][nindex])
          //   console.log(arr)
          //   that.setData({
          //     codeContent: arr,
          //     codeTotal: codetotal
          //   })
          //   console.log(that.data.codeContent)
          //   console.log('codeTotal:' + that.data.codeTotal)
          // }else{
          //   util.showErrorToast(obj.errmsg)
          // }
        });


      },
      fail() {
        util.showErrorToast('扫码失败')
      }
    })

  },
  //确认发货
  comfirmSendGoods: function() {
    let total = 0
    let remarks = wx.getStorageSync('sellRemarks')
    let orderInfo = wx.getStorageSync('orderInfo')
    let url = ''//请求的路径
    console.log('antiList:*****')
    console.log(this.data.antiIds)
    console.log(typeof this.data.antiIds[0])
    let temp = this.data.antiIds
    console.log(temp)
    this.data.orderGoods.forEach(obj => {
      total += obj.number
    })
    console.log('remarks:' + remarks)
    console.log('total:' + total)
    console.log('codeTotal:' + this.data.codeTotal)
    console.log('orderId：' + this.data.orderGoods[0].orderId)
    if (total != this.data.codeTotal) {
      util.showErrorToast('信息不完整！')
      return
    }
    if (orderInfo.payId == 'pur') {
      url = api.PurchaseSendGood
      console.log('转采购后')
    }else{
      url = api.SaleSendGood
      console.log('未转采购')
    }
    util.request(url, {
      id: this.data.orderGoods[0].orderId,
      sellRemarks: remarks,
      sysbatchIds: temp
    }, 'POST').then(data => {
      wx.showToast({
        title: '发货成功',
        icon: 'success'
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/sale/orderList/orderList?showType=' + 3,
        })
      }, 2000)
    })
  },
  // 确认收货 
  confirmDeliverGoods: function(e) {
    let total = 0
    this.data.orderGoods.forEach(obj => {
      total += obj.number
    })
    console.log('orderId：' + this.data.orderGoods[0].orderId)
    if (total != this.data.codeTotal) {
      util.showErrorToast('信息不完整！')
      return
    }
    util.request(api.ConfirmReceivingGoods, {
      id: this.data.orderGoods[0].orderId,
      orderStatus: 104
    }, 'POST').then(data => {
      wx.showToast({
        title: '收货成功',
        icon: 'success'
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/ucenter/index/index',
          success: function() {
            console.log('收货成功！')
          }
        })
      }, 2000)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let goods = wx.getStorageSync('orderGoods');
    let nopur = options.nopur
    console.log('nopur:' + nopur)
    console.log(goods)
    this.setData({
      orderGoods: goods,
      nopur: nopur
    })
    if (this.data.nopur == 'nopur') {
      wx.setNavigationBarTitle({
        title: '扫码收货',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})